from __future__ import division
import numpy as np
import scipy.stats as stats
import random


class Player:
    '''
    The algorithm simply calculates which quartile in the list of reputations does the opponent's reputation belong.
    Each quartile range constitutes a probabilistic decision for the player's action.
    Example: If the opponent's reputation is very far away from the median value, the player is expected to slack.
    Note that quartile values are used due to its robust nature.
    
    Instead of using the actual reputation of the opponent, a beta distributed random variable conditioned using the opponents 
    hunting history is used. This allows the prediction to account for variance.
    
    The equilibrium value of the player's reputation is likely to converge at around 0.3-0.4.
    This value will surely decrease when the average reputation of the tribe decreases.
    '''
    def __init__(self):
        pass
        
    def hunt_choices(self, round_number, current_food, current_reputation, m, player_reputations):        
        hunt_decisions = []
        
        players_median = np.median(player_reputations)

        r1 = stats.scoreatpercentile(player_reputations, 25)
        r3 = stats.scoreatpercentile(player_reputations, 75)
        
        per90 = stats.scoreatpercentile(player_reputations, 90)
        per10 = stats.scoreatpercentile(player_reputations, 10)
        per80 = stats.scoreatpercentile(player_reputations, 80)
        per20 = stats.scoreatpercentile(player_reputations, 20)
        per70 = stats.scoreatpercentile(player_reputations, 70)
        per30 = stats.scoreatpercentile(player_reputations, 30)
        per60 = stats.scoreatpercentile(player_reputations, 60)
        per40 = stats.scoreatpercentile(player_reputations, 40)

        for rep in player_reputations:
            if (rep == 0 and round_number<2):
                rand = np.random.random()
                if rand < 0.5:
                    hunt_decisions.append('h')
                else:
                    hunt_decisions.append('s')
                continue
            if ((rep >= per90) or (rep <= per10)):
                hunt_decisions.append('s')
            elif ((rep >= per80) or (rep <= per20)):
                rand = np.random.random()
                if ((rand < stats.beta.rvs(10*rep, 10 - 10*rep)) and ((current_reputation < rep) and ((current_reputation + (r3-r1)) > rep))):
                    hunt_decisions.append('h')
                else:
                    hunt_decisions.append('s')
            elif ((rep >= per70) or (rep <= per30)):
                rand = np.random.random()
                if ((rand < stats.beta.rvs(10*rep, 10 - 10*rep)) and (current_reputation < rep)):
                    hunt_decisions.append('h')
                else:
                    if np.random.random() < 0.4:
                        hunt_decisions.append('h')
                    else:
                        hunt_decisions.append('s')
            elif ((rep >= per60) or (rep <= per40)):
                if np.random.random() < 0.5:
                    hunt_decisions.append('h')
                else:
                    hunt_decisions.append('s')
            else:
                if np.random.random() < 0.6:
                    hunt_decisions.append('h')
                else:
                    hunt_decisions.append('s')  
            
        return hunt_decisions;

    def hunt_outcomes(self, food_earnings):
        pass 


    def round_end(self, award, m, number_hunters):
        pass